﻿/*
Copyright (c) 2011 Vratislav Cermak (vratacermak@gmail.com)
 
This file is part of TeachMe Project. See http://code.google.com/p/teach-me for more information.
 
TeachMe is an open source project. Using of TeachMe is free of charge.
It is distributed under GNU General Public License version 3 (GPLv3).
GPLv3 can be found at http://www.gnu.org/licenses/gpl.html.
 
This program is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE.  See the license for more details.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TeachMe.Application.Properties;

namespace TeachMe.Application
{
    public class ErrorProviders
    {
        private class ErrorProviderInfo
        {
            public ErrorProvider Provider { get; set; }
            public bool Used { get; set; }
        }

        private List<ErrorProviderInfo> _errors = new List<ErrorProviderInfo>();

        public void AddError(Control ctrl)
        {
            foreach (ErrorProviderInfo error in _errors)
            {
                if (!error.Used)
                {
                    error.Provider.SetError(ctrl, Resources.StrictEditInvalidValue);
                    error.Used = true;
                    return;
                }
            }

            ErrorProvider provider = new ErrorProvider {BlinkStyle = ErrorBlinkStyle.NeverBlink};
            provider.SetError(ctrl, Resources.StrictEditInvalidValue);

            _errors.Add(new ErrorProviderInfo { Provider = provider, Used = true });
        }

        public void Clear()
        {
            foreach (ErrorProviderInfo error in _errors) 
            {
                error.Provider.Clear();
                error.Used = false;
            }
        }

        public int Count
        {
            get
            {
                return _errors.Count(x => x.Used == true);
            }
        }
    }
}
